<script>

import { objectAssign, formatDate } from "../../utils/common";
import { server_request } from "../../utils/request";
import ServerUpload from "./ServerUpload";
import uploadConfig from "../../config/upload";
import OSS from "ali-oss";
import ossUpload from "../../utils/upload/oss";

/**
 *
    文件上传 - 阿里云OSS
    使用前请先安装 ali-oss。
    安装命令：
    npm install ali-oss

    同时oss要配置跨域，配置方式见下方文档
    https://help.aliyun.com/document_detail/32069.html?spm=a2c4g.11186623.6.1313.3d7f5966MCG1lX

 * 
 */
export default {
    extends: ServerUpload,
    name: "PopoOssUpload",
    data(){
        return {
            sts: null,
            stsLoaded: false,
        };
    },
    created(){
        this.opt.action = "";
        this.opt.httpRequest = this.httpRequest;
        this.sts = this.item.sts;
    },
    methods: {
        /**
         * 上传OSS请求
         * 
         * @param  {[type]} http [description]
         * @return {[type]}      [description]
         */
        httpRequest( http){
            /*
            http: {
                action: ""
                data: undefined
                file: File {uid: 1595394989063, name: "123.jpg", webkitRelativePath: "", …}
                filename: "file"
                headers: {__ob__: Observer}
                onError: ƒ onError(err)
                onProgress: ƒ onProgress(e)
                onSuccess: ƒ onSuccess(res)
                withCredentials: false
            }                
             */
            ossUpload( 
                http.file, 
                {
                    sts: this.sts,
                    fileOnlinePath: this.getRandomFileInfo( http.file).final,
                    progress: (e)=>{
                        http.onProgress( { percent: e.percent});
                    },
                    success: (e)=>{
                        http.onSuccess(e)
                    },
                    failure: (e)=>{
                        console.error( "上传失败", e);
                        http.onError( "上传失败");
                    }
                }
            );

            // return;
            
            // // console.log( "httpRequest", arguments);
            // var oss_config = objectAssign( {}, uploadConfig.oss.upload_config);

            // /*
            //     优先使用stsToken的方式进行鉴权，可支持在 item.sts 中传入已有的数据，也可以
            //     通过 upload.js 中的 uploadConfig.oss.get_sts_token_url 的配置，通过
            //     ajax来获得 stsToken。 
            //     如果都没有，则使用最不安全的 accessKeyId/accessKeySecret 的直传模式。
            //  */
            // if( this.sts){
            //     // 使用 sts方式进行oss鉴权。
            //     oss_config.accessKeyId = this.item.sts.credentials.AccessKeyId;
            //     oss_config.accessKeySecret = this.item.sts.credentials.AccessKeySecret;
            //     oss_config.stsToken = this.item.sts.credentials.SecurityToken;

            //     this.doUpload( oss_config, http);

            // }else if( !!uploadConfig.oss.get_sts_token_url && !this.stsLoaded){

            //     // 获得ajax方式获得 sts 数据
            //     server_request( uploadConfig.oss.get_sts_token_url).then( (res) => {
            //         // 防止反复调用 sts 接口
            //         this.stsLoaded = true;
            //         if( res.code == 200){
            //             this.sts = res.data;
            //             this.httpRequest( http);
            //         }else{
            //             this.$message.error( "获取OSS临时凭证失败");
            //         }
            //     });

            // }else{
            //     this.doUpload( oss_config, http);
            // }

        },
        /*
        doUpload( oss_config, http){
            console.log( "oss_config", oss_config);
            let client = new OSS( oss_config);

            try{
                let tempCheckpoint = null;
                let fileInfo = this.getRandomFileInfo( http.file);
                let fileFinalPath = fileInfo.final;

                let success_url = ( [
                    "http",
                    ( oss_config.secure ? "s" : ""),
                    "://",
                    oss_config.bucket,
                    ".",
                    oss_config.endpoint,
                    fileFinalPath
                ]).join( "");

                // console.log( "success_url", success_url);

                let result = client.multipartUpload( fileFinalPath, http.file, {
                    progress: function (p, checkpoint, res) {
                        http.onProgress( { percent: Math.floor(p * 100)});
                    }
                });
                result.then( ( res) => {
                    // console.log( "upload success", res, success_url);
                    http.onSuccess( {
                        code: 200,
                        msg: "success",
                        data: success_url
                    });
                });
            }catch( e){
                http.onError( "上传失败");
                client.cancel();
                console.error( e);
            }
        },
        */
    }
};
</script>

